import React from 'react';
import {
  View,
  ScrollView,
  TouchableOpacity,
  Platform,
  Alert,
} from 'react-native';
import {Text, Button, ListItem} from '@rneui/base';
import {
  useMap,
  useMount,
  useSafeState,
  useSetState,
  useUpdateEffect,
} from 'ahooks';
import {round} from 'lodash';
import style from './style.less';
import {
  requestMultiple,
  checkMultiple,
  PERMISSIONS,
  RESULTS,
  openSettings,
  request,
} from 'react-native-permissions';
import {useNavigation} from '@react-navigation/native';
import {FileSaveDirName} from '@/config/common';
import dayjs from 'dayjs';
import ImagePicker from 'react-native-image-crop-picker';
import {Dirs, FileSystem} from 'react-native-file-access';
import RNFS from 'react-native-fs';
import * as RNPath from 'react-native-path';
export {default as config} from './config';
export default props => {
  const navigation = useNavigation();
  return (
    <ScrollView style={style.box}>
      <Button
        title="普通拍照"
        onPress={async () => {
          try {
            let dir = `${Dirs.SDCardDir}/${FileSaveDirName}/${dayjs().format(
              'YYYYMM',
            )}`;
            let filename = `${dayjs().format('YYYYMMDDHHmmssSSS')}.txt`;
            let savePath = `${dir}/${filename}`;
            let info = RNPath.parse(savePath);
            await RNFS.mkdir(info.dir);
            let sRes = await RNFS.writeFile(savePath, Math.random() + 'ABCDE');
            let gRes = await RNFS.readFile(savePath);
            console.log('savePath', savePath);
            console.log('gRes', gRes);
          } catch (e) {
            console.log('异常', e.message);
          }
        }}
      />
      <Button
        title="裁剪100*100"
        containerStyle={style.marginTop10}
        onPress={async () => {
          try {
            let tmpFileInfo = await ImagePicker.openCamera({});
            let parseInfo = RNPath.parse(tmpFileInfo.path);
            let newDir = `${Dirs.SDCardDir}/${FileSaveDirName}/image`;
            let newFilename = `${dayjs().format('YYYYMMDDHHmmssSSS')}${
              parseInfo.ext
            }`;
            await RNFS.mkdir(newDir);
            let newPath = `${newDir}/${newFilename}`;
            let binary = await RNFS.readFile(tmpFileInfo.path, 'base64');
            await RNFS.writeFile(newPath, binary, 'base64');
            console.log('newPath', newPath, binary.length);
          } catch (e) {
            console.log('异常', e.message);
          }
        }}
      />

      <Button
        title="打开录像"
        containerStyle={style.marginTop10}
        onPress={async () => {
          try {
            let tmpFileInfo = await ImagePicker.openCamera({
              mediaType: 'video',
            });
            let parseInfo = RNPath.parse(tmpFileInfo.path);
            let newDir = `${Dirs.SDCardDir}/${FileSaveDirName}/video`;
            let newFilename = `${dayjs().format('YYYYMMDDHHmmssSSS')}${
              parseInfo.ext
            }`;
            await RNFS.mkdir(newDir);
            let newPath = `${newDir}/${newFilename}`;
            let binary = await RNFS.readFile(tmpFileInfo.path, 'base64');
            await RNFS.writeFile(newPath, binary, 'base64');
            console.log('newPath', newPath, binary.length);
          } catch (e) {
            console.log('异常', e.message);
          }
        }}
      />
    </ScrollView>
  );
};
